package edu.task.snake.view;

import edu.task.snake.controller.GameEngine;

import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;


public class ViewArena extends JPanel implements ActionListener {
    private List<FrogRender> frogRenderList;
    private SnakeRender snakeRender;
    public ViewArena(int width, int height) {
        setFocusable(true);
        setPreferredSize(new Dimension(width, height));
    }

    public List<FrogRender> getFrogRenderList() {
        return frogRenderList;
    }

    public void setFrogRenderList(List<FrogRender> frogRenderList) {
        this.frogRenderList = frogRenderList;
    }

    public SnakeRender getSnakeRender() {
        return snakeRender;
    }

    public void setSnakeRender(SnakeRender snakeRender) {
        this.snakeRender = snakeRender;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (FrogRender frogRender : frogRenderList) {
            frogRender.render(g);
        }
        snakeRender.render(g);
     // ToDo repaint Characters or arena object? Or oly repaint only one character old coords need saved

    }


    public void gameOver() {
        String msg = "Game Over";
        Font small = new Font("Helvetica", Font.BOLD, 14);
        FontMetrics metr = getFontMetrics(small);
        Graphics g = getGraphics();
        g.setColor(Color.white);
        g.setFont(small);
        g.drawString(msg, (this.getWidth() * GameEngine.getCellSizePixel() - metr.stringWidth(msg)) / 2, this.getHeight() * GameEngine.getCellSizePixel() / 2); //todo not work
    }

    public void actionPerformed(ActionEvent e) {
        paintComponent(this.getGraphics());
    }
}
